package com.glory.erp.auth.menu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.glory.erp.auth.menu.service.MenuService;
import com.glory.erp.auth.menu.bean.Menu;
import com.glory.erp.auth.menu.bean.MenuQuery;
import com.glory.erp.auth.role.bean.Role;
import com.glory.erp.auth.role.service.RoleService;
import com.glory.erp.auth.base.BaseAction;

/**
 * 
 * @Description: 菜单
 * @Title: MenuAction.java
 * @Author: 樊双峰
 * @Date: 2018年5月24日 下午10:26:11
 * @Version V1.0
 */
public class MenuAction extends BaseAction{
	
	public Menu menu = new Menu();
	public MenuQuery menuQuery = new MenuQuery();
	public Long[] roleUuids;
	
	private MenuService menuService;
	
	private RoleService roleService;
	
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	/**
	 * 
	 * @Description: 菜单展示列表
	 * @Title: list
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月24日 下午10:26:37
	 * @throws
	 */
	public String list(){
		List<Menu> parentList = menuService.findAllOneLevel();
		put("parentList", parentList);
		setDataTotal(menuService.getCount(menuQuery));
		List<Menu> menuList = menuService.findAll(menuQuery,pageNum,pageCount);
		put("menuList", menuList);
		return LIST;
	}

	/**
	 * 
	 * @Description: 跳转至添加、修改页面
	 * @Title: addView
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月24日 下午10:26:53
	 * @throws
	 */
	public String addView(){
		List<Role> roleList = roleService.findAll();
		put("roleList", roleList);
		List<Menu> menuList = menuService.findAllOneLevel();
		put("menuList", menuList);
		if(menu.getUuid()!=null){
			menu = menuService.findById(menu.getUuid());
			// set --> array
			roleUuids = new Long[menu.getRoles().size()];
			int i = 0;
			for (Role r : menu.getRoles()) {
				roleUuids[i++] = r.getUuid();
			}
		}
		return ADD;
	}

	/**
	 * 
	 * @Description: 添加、修改菜单
	 * @Title: saveAndUpdate
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月24日 下午10:27:18
	 * @throws
	 */
	public String saveAndUpdate(){
		if(menu.getUuid() == null){
			menuService.save(menu,roleUuids);
		}else{
			menuService.update(menu,roleUuids);
		}
		return TO_LIST;
	}

	/**
	 * 
	 * @Description: 删除菜单
	 * @Title: delete
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月24日 下午10:27:36
	 * @throws
	 */
	public String delete(){
		menuService.delete(menu);
		return TO_LIST;
	}
	
	public void showMenu() throws IOException{
		String root = getRequest().getParameter("root");
		HttpServletResponse response = getResponse();
		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = response.getWriter();
		StringBuilder json = new StringBuilder();
		json.append("[");
		if("source".equals(root)){
			// 一级菜单
			List<Menu> menuList = menuService.findAllOneLevelByEmp(getLogin().getUuid());
			for (Menu m : menuList) {
				json.append("{\"text\":\"");
				json.append(m.getName());
				json.append("\",\"hasChildren\":true,\"classes\":\"folder\",\"id\":\"");
				json.append(m.getUuid());
				json.append("\"},");
			}
		} else {
			// 二级菜单
			// 获取指定一级菜单所对应的二级菜单
			Long puuid = new Long(root);
			List<Menu> menuList = menuService.findByEmpAndPuuid(getLogin().getUuid(),puuid);
			for (Menu m : menuList) {
				json.append("{\"text\":\"<a class='hei' target='main' href='");
				json.append(m.getUrl());
				json.append("'>");
				json.append(m.getName());
				json.append("</a>\",\"hasChildren\":false,\"classes\":\"file\"},");
			}
		}
		json.deleteCharAt(json.length()-1);
		json.append("]");
		writer.print(json.toString());
		writer.flush();
	}
}